//
//  DFPageController.m
//  Pods
//
//  Created by rujia chen on 17/4/12.
//
//

#import "DFPageController.h"
#import "Masonry.h"
#import "DFPageHeadView.h"
#import "UIView+DFExtension.h"
#import "UIViewController+DFExtension.h"
#import <UIColor+DFExtension.h>

@interface DFPageController ()<UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@property (nonatomic, strong) UIPageViewController *pageViewController;

@end

@implementation DFPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    [self.view addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo([self df_shouldHideNavigationBar]?self.mas_topLayoutGuideTop:self.mas_topLayoutGuideBottom);
    }];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self.headView setInsets:UIEdgeInsetsMake([self df_shouldHideNavigationBar]?[self.topLayoutGuide length]:0, 0, 0, 0)];
    [self.headView invalidateIntrinsicContentSize];
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    _currentIndex = currentIndex;
    [self.headView setIndex:currentIndex animated:YES];
}

- (NSString *)title {
    return @"测试";
}

#pragma mark - UIPageViewControllerDataSource

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger pageIndex = [[self viewControllers] indexOfObject:viewController];
    return pageIndex > 0 ? [self viewControllers][pageIndex - 1]: nil;
}

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger pageIndex = [[self viewControllers] indexOfObject:viewController];
    return pageIndex < [[self viewControllers] count] - 1 ? [self viewControllers][pageIndex + 1]: nil;
}

#pragma mark - UIPageViewControllerDelegate

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    NSInteger index = [self.viewControllers indexOfObject:pageViewController.viewControllers[0]];
    self.currentIndex = index;
}

#pragma mark - getter

- (UIPageViewController *)pageViewController {
    if (!_pageViewController) {
        _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
        
        _pageViewController.delegate = self;
        _pageViewController.dataSource = self;
        
        [_pageViewController setViewControllers:@[self.viewControllers[self.currentIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
            
        }];
    }
    
    return _pageViewController;
}

- (DFPageHeadView *)headView {
    if (!_headView) {
        _headView = [[DFPageHeadView alloc] init];
        _headView.titleColor = self.df_navigationBarForegourndColor;
        [_headView df_setIntrinsicContentSizeBlock:^CGSize(UIView *view) {
            CGFloat height = [self headHeight] + ([self df_shouldHideNavigationBar]?[self.topLayoutGuide length]:0);
            return CGSizeMake(UIViewNoIntrinsicMetric, height);
        }];
        [_headView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        
        
        NSMutableArray <NSString *>*titles = [[NSMutableArray alloc] init];
        [self.viewControllers enumerateObjectsUsingBlock:^(UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [titles addObject:obj.title?:@"empty"];
        }];
        
        [_headView setTitleButtonSpacing:20];
        [_headView setTitles:titles];
        _headView.backgroundVisulEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
        UIToolbar *toolbar = [[UIToolbar alloc] init];
        toolbar.barTintColor = [self df_navigationBarTintColor];
        _headView.backgroundView = toolbar;
        __weak typeof(self) weakSelf = self;
        _headView.tabClickBlock = ^(NSInteger index) {
//            UIViewController *fromVC = [self.pageViewController.childViewControllers lastObject];
//            UIViewController *toVC = self.viewControllers[index];
            __strong typeof(weakSelf) strongSelf = weakSelf;
            UIPageViewControllerNavigationDirection direction = strongSelf.currentIndex < index ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
            strongSelf.currentIndex = index;
            [strongSelf.pageViewController setViewControllers:@[strongSelf.viewControllers[strongSelf.currentIndex]] direction:direction animated:YES completion:^(BOOL finished) {
                
            }];
        };
        [_headView setIndex:self.currentIndex animated:NO];
    }
    
    return _headView;
}

- (CGFloat)headHeight {
    return 44;
}


@end
